Spring Cloud OpenFeign Interceptor#
Request Interceptor Concept#
HTTP Interceptorsallow us to check or modify all the incoming or outgoing HTTP requests in our application. We can use them if we want to apply something like an authorization header to all the requests.- There are many use-cases where
HTTP Interceptorscan come in handy. Since they intercept all the API calls, we can use them to apply some logic before or after the API call. Theseinterceptorshave access to the complete request and response body. So, if we want to apply some logic to all the requests, we can add it once using aninterceptorinstead of writing separate code for every request. Here are some examples of things we can do.- Apply an authorization header to all the requests.
- Prefix all the requests with the server name.
- Log all the requests and responses.
- Set a global error catch.
- Resend the request if it fails.
Basic Auth Request Interceptor#
- In
spring-cloud-starter-openfeigndependency , we have been already provided theBasicAuthRequestInterceptorfor supporting calling to apis which require the basic authentication token. - So, let's start with the example below then we can understand it easily.
Prepare#
- So to begin with the example, we need to prepare some application services as below:
- In this image we will use
Postmanto call to theClient Applicationthat we will configure below, then from this Client Application, it will call to theResource Applicationwhich is configured with Basic Authentication. - For the source code of
Resource Applicationyou can download here. - Now, let's create and configure the
Client Applicationas below.
Dependencies#
- To use
BasicAuthRequestInterceptorinOpenFeign, you need to import the dependency below into thepom.xmlof yourClient Applicationwhich is a Spring Boot application.
| pom.xml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 | |
Create Feign Configuration#
- Let's create a configuration class with name
BasicAuthFeignConfigfor configuringBasicAuthRequestInterceptoras below:
| BasicAuthFeignConfig.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
- As you can see, we will create a bean
BasicAuthRequestInterceptorwith theusernameandpasswordthat we load from theapplication.yml.
| application.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | |
- If you don't want to apply the
BasicAuthRequestInterceptorfor all FeignClients, so please don't put the annotation@Configurationin classBasicAuthFeignConfig.java.
FeignClient Configuration#
- Now we will create an adapter interface name
SpringBasicAuthClientto configure FeignClient with target api as below:
| SpringBasicAuthClient.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
- As we can see, in the
@FeignClientannotation, we will provide a client name, the server domain URL that we need to call to and we will load theBasicAuthRequestInterceptorconfiguration class that we want to apply for thisFeignClientby using paramconfiguration = {BasicAuthFeignConfig.class}.
Controller#
- Now, we need to create a simple controller as below for testing with Postman.
| OpenFeignInterceptorController.class | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | |
Testing#
- Finally, let's start our
Client ApplicationandResource Application, then you postman to call the exported api above. Then you should see the 200 result as below.
- Then if you check the log in the console, you can see
Authorizationheader had been added into the request to theResource Applicationfor getting the data.
Header Interceptor#
- In some cases, we might need to add some custom request headers into our requests for calling apis. In this case we would like to use
RequestInterceptorof OpenFeign which will give us more options for configuring Request Interceptor.
Prepare#
- Like the example above, we also follow the diagram and services that we created. But when we test with the postman, we will look into the console log to check our custom headers have been added into the requests to
Resource Server
Create Feign Configuration#
- Let's create a configuration class with name
FeignHeaderConfigfor configuringRequestInterceptoras below:
| FeignHeaderConfig.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | |
- As you can see, we will create a bean
RequestInterceptorwith some example headers and an authorization token that we load from theapplication.yml.
| application.yml | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |
FeignClient Configuration#
- Now we will create an adapter interface name
SpringCustomHeaderClientto configure FeignClient with target api as below:
| SpringCustomHeaderClient.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
- Like the
FeignClientconfiguration that we did before inSpringBasicAuthClient, now we just need to change the name and use paramconfiguration = {FeignHeaderConfig.class}to load theFeignHeaderConfigwhich will be applied for thisFeignClient.
Controller#
- Now, we need to add a new api into the controller that we created before.
| OpenFeignInterceptorController.java | |
|---|---|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | |
Testing#
- Finally, let's start our
Client ApplicationandResource Application, then you postman to call the exported api above. Then you should see the 200 result as below.
- Then if you check the log in the console, you can see all your custom headers had been added into the request to the
Resource Applicationfor getting the data.
OpenFeign Interceptor With OAuth2 Client#
- Please view sections below:




